home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / copydeepmask / copydeepmask.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.1 KB  |  242 lines

  1. /*
  2.     File:        CopyDeepMask.c
  3.  
  4.     Contains:    This snippet demonstrates the use of CopyDeepMask using 2 PICTS;
  5.                  one a photograph and the other a triangular mask.     It uses 2 offscreen
  6.                  gworlds to hold the source and mask pixmaps.  CopyDeepMask is then used
  7.                  to create the masked image and display it in the application window.  The
  8.                  source, mask, and destination rectangles are all the same size in order
  9.                  avoid altering pixel sizes.
  10.  
  11.     Written by: Kevin Mellander    
  12.  
  13.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  25.                 12/14/94     Kevin Mellander    Fixed a bug in doEventLoop that was causing a bus error using 
  26.                                              EvenBetterBusError (bad things happen when you attempt to use an 
  27.                                              unitialized EventRecord!)
  28.                 11/22/93    Kevin Mellander    Created        
  29.                 
  30.  
  31. */
  32. #include <Quickdraw.h>
  33. #include <QDOffscreen.h>
  34. #include <Windows.h>
  35. #include <Dialogs.h>
  36. #include <Menus.h>
  37. #include <Types.h>
  38. #include <Memory.h>
  39. #include <Fonts.h>
  40. #include <OSEvents.h>
  41. #include <ToolUtils.h>
  42.  
  43. #define windID 128
  44. #define inFront -1
  45. #define sleepTime 30
  46.  
  47. #define     srcPicID    130
  48. #define     maskPicID    131
  49.  
  50. Boolean                    continueThis;
  51. WindowPtr                mainWindow;
  52.  
  53. void InitToolbox(void);
  54. void doEventLoop(void);
  55. void doDrag (WindowPtr winPtr,Point mouseLoc);
  56. void doUpdateEvent(EventRecord *event);
  57.  
  58.  
  59. void main(void)
  60. {
  61.     
  62.     InitToolbox();
  63.         
  64.     mainWindow = GetNewCWindow(windID,nil,(WindowPtr)inFront);
  65.     
  66.     continueThis = true;
  67.     
  68.     doEventLoop();
  69. }
  70.  
  71. void InitToolbox(void)
  72. {
  73.     MoreMasters();
  74.     MoreMasters();
  75.     MoreMasters();
  76.     
  77.     MaxApplZone();
  78.  
  79.     InitGraf((Ptr)&qd.thePort);
  80.     InitFonts();
  81.     InitWindows();
  82.     InitMenus();
  83.     TEInit();
  84.     InitCursor();
  85.     InitDialogs((long)nil);
  86.     FlushEvents(everyEvent, 0);
  87.     
  88. }
  89.  
  90. void doEventLoop(void)
  91. {
  92.     EventRecord    event;
  93.     WindowPtr    window;
  94.     short        hitArea;
  95.         
  96.     
  97.     while (continueThis)
  98.     {    
  99.         if (WaitNextEvent(everyEvent,&event,sleepTime,nil))
  100.             
  101.             if (event.what == updateEvt)    
  102.                 doUpdateEvent(&event);
  103.                 
  104.             else if (event.what == mouseDown)
  105.             {
  106.                 hitArea = FindWindow(event.where,&window);
  107.                 
  108.                 if (hitArea == inDrag)
  109.                     doDrag(window,event.where);
  110.                     
  111.                 else if (hitArea == inGoAway)
  112.                     if (TrackGoAway (window,event.where))
  113.                         return;
  114.             }
  115.             
  116.     } 
  117. }
  118.         
  119. void doDrag (WindowPtr winPtr,Point mouseLoc)
  120. {
  121.     Rect     dragRect;
  122.     Rect    bndsRect;
  123.     
  124.     bndsRect = qd.screenBits.bounds; /*screenBits is a QuickDraw global variable with the same structure as portBits (BitMap)*/
  125.     InsetRect(&dragRect,10,10);
  126.     DragWindow(winPtr,mouseLoc,&bndsRect);
  127.     
  128. }
  129.  
  130. void doUpdateEvent(EventRecord *event)
  131. {
  132. #pragma unused (event)
  133.  
  134.     Rect            bndsRectSrc,bndsRectMask,srcRect,maskRect,destRect;
  135.     QDErr            gWorldErr;
  136.     GWorldPtr        offscreenGWorldSource;
  137.     GWorldPtr        offscreenGWorldMask;
  138.     short            pixelDepthSource = 32;
  139.     short            pixelDepthMask = 1;
  140.     GWorldFlags        flags = 0;
  141.     GDHandle        currGDevice;
  142.     GWorldPtr        currGWorldPort;
  143.     PicHandle        srcPicHdl;
  144.     PicHandle        maskPicHdl;
  145.     PixMapHandle    srcPMHdl;
  146.     PixMapHandle    maskPMHdl;
  147.     Boolean            pmLock= false;
  148.  
  149.     
  150.     //Set up the bounds rectangle for the source and mask gWorlds    
  151.     bndsRectSrc.top = 32;
  152.     bndsRectSrc.left = 64;
  153.     bndsRectSrc.bottom = 160;
  154.     bndsRectSrc.right = 192;
  155.     
  156.     bndsRectMask = bndsRectSrc;
  157.     
  158.     //Make the source,mask and destination rectangles the same 
  159.     // size as the associated gWorlds
  160.     srcRect = bndsRectSrc;
  161.     maskRect = bndsRectSrc;
  162.     destRect = bndsRectSrc;
  163.     
  164.     //Fetch the current port and gdevice and save them for later
  165.     GetGWorld(&currGWorldPort,&currGDevice);
  166.         
  167.     //Set up our source and mask gWorlds
  168.     gWorldErr = NewGWorld(&offscreenGWorldSource,pixelDepthSource,&bndsRectSrc,0,nil,flags);
  169.     if(gWorldErr != noErr)
  170.         DebugStr("\pthe first NewGWorld call failed");
  171.     else 
  172.     {
  173.         //lock the offscreen buffer
  174.         srcPMHdl = GetGWorldPixMap(offscreenGWorldSource);
  175.         pmLock = LockPixels(srcPMHdl);
  176.         if (!pmLock)
  177.             DebugStr("\pthe first LockPixels call failed");
  178.     }
  179.         
  180.     gWorldErr = NewGWorld(&offscreenGWorldMask,pixelDepthMask,&bndsRectMask,0,nil,flags);
  181.     if(gWorldErr != noErr)
  182.         DebugStr("\pthe second NewGWorld call failed"); 
  183.     else 
  184.     {
  185.         //lock the offscreen buffer
  186.         maskPMHdl = GetGWorldPixMap(offscreenGWorldMask);
  187.         pmLock = LockPixels(maskPMHdl);
  188.         if (!pmLock)
  189.             DebugStr("\pthe second LockPixels call failed");
  190.     }
  191.         
  192.     //Set the current graphics world to my offscreen source and draw into it
  193.     SetGWorld((CGrafPtr) offscreenGWorldSource,nil);
  194.     
  195.     srcPicHdl = GetPicture(srcPicID);
  196.     if (srcPicHdl==nil)
  197.         DebugStr("\pthe first call to GetPicture failed");
  198.     EraseRect(&srcRect);
  199.     HLock((Handle)srcPicHdl);
  200.     DrawPicture(srcPicHdl,&srcRect);
  201.     HUnlock((Handle) srcPicHdl);
  202.     
  203.     //Set the current graphics world to my offscreen mask and draw into it
  204.     SetGWorld((CGrafPtr)offscreenGWorldMask,nil);
  205.     GetPicture(maskPicID);
  206.     maskPicHdl = GetPicture(maskPicID);
  207.     if (maskPicHdl==nil)
  208.         DebugStr("\pthe second call to GetPicture failed");
  209.     EraseRect(&maskRect);
  210.     HLock((Handle)maskPicHdl);
  211.     DrawPicture(maskPicHdl,&maskRect);
  212.     HUnlock((Handle)maskPicHdl);
  213.  
  214.     //Set the current graphics port to my application window for drawing into
  215.     SetGWorld((CGrafPtr)mainWindow,nil);
  216.     BeginUpdate(mainWindow);
  217.  
  218.     //Now for the whole point of this. . .call CopyDeepMask
  219.     CopyDeepMask(
  220.                 (BitMap *) &(offscreenGWorldSource->portPixMap),
  221.                 (BitMap *) &(offscreenGWorldMask->portPixMap),
  222.                 &(mainWindow->portBits),
  223.                 &srcRect,
  224.                 &maskRect,
  225.                 &destRect,
  226.                 srcCopy,
  227.                 nil);
  228.                 
  229.     EndUpdate(mainWindow);
  230.     
  231.     //Unlock the offscreen buffer
  232.     UnlockPixels(srcPMHdl);
  233.     UnlockPixels(maskPMHdl);
  234.     
  235.     //Restore the saved port and gdevice
  236.     SetGWorld(currGWorldPort,currGDevice);
  237.         
  238.     //Dispose of the gWorlds
  239.     DisposeGWorld(offscreenGWorldSource);
  240.     DisposeGWorld(offscreenGWorldMask);
  241.  
  242. }